package digital.store.springmvc.controller;




import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import digital.store.springmvc.service.CategoryService;
import digital.store.springmvc.service.ProductService;

import digital.store.tutorial.springmvc.model.Category;
import digital.store.tutorial.springmvc.model.Product;

@Controller
@RequestMapping(value="/product-detail")
public class ProductDetailController {
	@Autowired
	private ProductService productService;
	@Autowired
	private CategoryService categoryService;
	
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public String showEditForm(@PathVariable Long id, Model model) {
		
		Collection<Category> categories = categoryService.findAll();
		Product product = productService.findById(id);
		Category category = product.getCategory();
		
		
        model.addAttribute("productDetail",product);
        model.addAttribute("category", category);
        model.addAttribute("categoriesSidebar", categories);
        
        return "home/product-detail";
    }
}
